package com.example.curator.base;

import com.example.curator.CuratorClientUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.transaction.CuratorOp;
import org.apache.curator.framework.api.transaction.CuratorTransactionResult;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.Stat;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author majie
 * @date 2018/6/29.
 */
public class CuratorOperatorTest {

    private static CuratorFramework curatorFramework;

    private static final String PATH = "/path";

    @BeforeClass
    public static void getInstance( ) {
        curatorFramework = CuratorClientUtils.getInstance();
        System.out.println("连接成功.........");
    }

    @Test
    public void create( ) throws Exception {
        String s = curatorFramework.create()
                .withMode(CreateMode.PERSISTENT)   //持久节点
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)   //权限
                .forPath(PATH);         //节点名称,不存数据默认存ip地址
        System.out.println(s);
    }

    /**
     * curatorFramework.getChildren().forPath(PATH);
     * 获取所有子节点
     */
    @Test
    public void get( ) throws Exception {
        Stat stat = curatorFramework.checkExists().forPath(PATH);
        if (stat != null) {
            String s = stat.toString();
            System.out.println(s);
            byte[] bytes = curatorFramework.getData().forPath(PATH);
            System.out.println(new String(bytes));
        }
        curatorFramework.getChildren().forPath(PATH);
    }

    @Test
    public void update( ) throws Exception {
        curatorFramework.setData().forPath(PATH, "123".getBytes());
        Stat stat = curatorFramework.checkExists().forPath(PATH);
        System.out.println(stat);
        byte[] bytes = curatorFramework.getData().forPath(PATH);
        System.out.println(new String(bytes));
    }


    @Test
    public void delete( ) throws Exception {
        //guaranteed() 解决在服务器上可能成功执行操作但在响应可成功返回给客户端之前发生连接失败的边缘情况。
        //deletingChildrenIfNeeded() 递归删除子节点
        curatorFramework.delete().guaranteed().forPath(PATH);
    }

    @Test
    public void listener( ) throws Exception {
        curatorFramework.getCuratorListenable().addListener((client, event) -> {
            WatchedEvent watchedEvent = event.getWatchedEvent();
            System.out.println(watchedEvent.toString());
        });

        curatorFramework.create().forPath(PATH, "123".getBytes());
    }

    /**
     * 事务
     */
    @Test
    public void transaction( ) throws Exception {
        CuratorOp createOp = curatorFramework.transactionOp().create().forPath(PATH, "some data".getBytes());
        CuratorOp setDataOp = curatorFramework.transactionOp().setData().forPath(PATH, "other data".getBytes());
        CuratorOp deleteOp = curatorFramework.transactionOp().delete().forPath(PATH);

        //后台处理事务.处理完成以后回调
        curatorFramework.transaction().inBackground((client, event) -> {
            //不用inBackground的话就写在外面
            for (CuratorTransactionResult result : event.getOpResults()) {
                System.out.println(result.getForPath() + " - " + result.getType());
            }
        }).forOperations(createOp, setDataOp, deleteOp);

        TimeUnit.SECONDS.sleep(3);
    }
}
